<!doctype html>
<html lang="zh-cmn-Hans">
<!--
 * DefaultGatewaysRead.html
 * 用户列表
 * @package Console
 * @author SunP<sunp@aowsp.com>
 * @copyright Copyright © Since 2012 SunP All Rights Reserved.
-->
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
	<meta name="renderer" content="webkit"/>
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
	<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
	<meta name="author" content="SunP,sunp@aowsp.com,SunQuan,sunquan@onlyedu.com"/>
	<link rel="stylesheet" href="<{$Frontend}>/Javascript/layui-v2.2.2/css/layui.css" media="all">
	<link rel="stylesheet" href="<{$Frontend}>/font-awesome/css/font-awesome.min.css" media="all">
	<link rel="stylesheet" href="<{$Frontend}>/Console/build/css/app.css" media="all">
	<link rel="stylesheet" href="<{$Frontend}>/Console/build/css/themes/orange.css" media="all">
	<style>
		.layui-table-view {
			margin:0;
		}
	</style>
</head>
<body class="kit-theme">

<div class="layui-form">
	<div class="layui-form-item" style="padding:10px 0 0 10px;margin-bottom: 10px">
		<div class="layui-input-inline">
			<input name="Phone" required lay-verify="required" placeholder="请输入电话" autocomplete="off" id="IndexPhone"
				   class="layui-input">
		</div>

		<div class="layui-input-inline">
			<button name="SPSubmit" lay-submit lay-filter="AddFormButton" class="layui-btn layui-btn-normal" value="1344" id="test">
				新增
			</button>
		</div>
	</div>
</div>
<table class="List" id="List" lay-filter="List"></table>
<script type="text/html" id='AddNewStuTpl'>
	<div style="margin:10px">
		<div class="layui-form" lay-filter="AddForm">
			<div class="layui-form-item">
				<label class="layui-form-label" style="text-align: left">学员姓名</label>
				<div class="layui-input-block">
					<input type="text" name="Form[Customer][CnName]" required lay-verify="required"
						   placeholder="请输入学员姓名" autocomplete="off" class="layui-input" value="{{d.CnName}}">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label" style="text-align: left">学员电话</label>
				<div class="layui-input-block">
					<input type="text" name="Form[Customer][Phone]" required lay-verify="required"
						   placeholder="请输入学员电话" autocomplete="off" class="layui-input" value="{{d.Phone}}">
				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label" style="text-align: left">选择年级</label>
				<div class="layui-input-block" id="Class">

				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label" style="text-align: left">选择校区</label>
				<div class="layui-input-block" id="School">

				</div>
			</div>
			<div class="layui-form-item">
				<label class="layui-form-label" style="text-align: left">单据类型</label>
				<div class="layui-input-block" id="Type" lay-filter = "Type">

				</div>
			</div>
			<div class="layui-form-item" style="display: none">
				<label class="layui-form-label" style="text-align: left">介绍人</label>
				<div class="layui-input-block">
					<div class="layui-inline" style="width: 100%">
						<input type="text" name="Form[InnerOwner]" class="layui-input" id="InnerOwner">
					</div>
				</div>
			</div>
			<div class="layui-form-item" style="display: none">
				<div class="layui-input-inline" style="margin-right: 0;float: right">
					<button name="SPSubmit" lay-submit lay-filter="AddButton" class="layui-btn layui-btn-normal">新增</button>
					<button class="layui-btn layui-btn-normal" id="AddCancel">取消</button>
				</div>
			</div>
			<div id="OldStu"></div>
		</div>
	</div>
</script>
<script type="text/html" id="ClassSelectTpl">
	{{#  layui.each(d, function(index, item){ }}
	{{# if(item.Checked == 1){ }}
	<input type="radio" name="Form[Grade]" value='{{item.UUID}}' title='{{item.CnName}}'>
	{{# }else{ }}
	<input type="radio" name="Form[Grade]" value='{{item.UUID}}' title='{{item.CnName}}'>
	{{# } }}
	{{#  }); }}
</script>
<script type="text/html" id="SchoolSelectTpl">
	{{#  layui.each(d, function(index, item){ }}
	{{# if(item.Checked == 1){ }}
	<input type="radio" name="Form[Group]" value='{{item.UUID}}' title='{{item.CnName}}'>
	{{# }else{ }}
	<input type="radio" name="Form[Group]" value='{{item.UUID}}' title='{{item.CnName}}'>
	{{# } }}
	{{#  }); }}
</script>
<script type="text/html" id="TypeSelectTpl">
	{{#  layui.each(d, function(index, item){ }}
	{{# if(item.Checked == 1){ }}
	<input type="radio" name="Form[Type]" value='{{item.UUID}}' title='{{item.CnName}}' lay-filter = "Type">
	{{# }else{ }}
	<input type="radio" name="Form[Type]" value='{{item.UUID}}' title='{{item.CnName}}' lay-filter = "Type">
	{{# } }}
	{{#  }); }}
</script>
<script type="text/html" id="IntroTpl">
	<div class="layui-form-item" id="AddNewDiv" style="float: left;margin-left: 15px;margin-top: 15px">
		<div class="layui-input-inline">
			<input required lay-verify="required" placeholder="请输入介绍人电话" autocomplete="off" class="layui-input" id="InnerPhoneInput">
		</div>
		<div class="layui-input-inline" style="width: auto;margin-right: 0">
			<button class="layui-btn layui-btn-normal" id="AddNew">校验</button>
		</div>
	</div>
	<label class="layui-form-label" id="JiaoYanLable" style="text-align: left;color:#888888;width: 160px;float: none"></label>
</script>

<script src="<{$Frontend}>/Javascript/jquery-3.2.1.min.js"></script>
<script src="<{$Frontend}>/Javascript/layui-v2.2.2/layui.js"></script>
<script src="<{$Frontend}>/Console/build/common/common.js"></script>

<script>
	$.when(
		$.ajax({url:"<{SPURL c = 'CRM' a = 'FlowsRead' SPSubmit=''}>"}),
		$.ajax({url:"<{SPURL c = 'Access' a = 'UsersRead' SPSubmit=''}>"})
	).then(function (FlowsReadData,UsersData){

		FlowsReadData = JSON.parse(FlowsReadData[0]);
		UsersData = JSON.parse(UsersData[0]);

		//查找工作流列表中执行人的汉语
		for(var i=0;i<UsersData.Data.length;i++){
			for(var a=0;a<FlowsReadData.Data.length;a++){
				if(FlowsReadData.Data[a].Executor == UsersData.Data[i].UUID){
					FlowsReadData.Data[a].ExecutorName = UsersData.Data[i].CnName;
				}
			}
		}
		layui.use(['layer', 'table', 'form', 'laytpl','laydate'],function () {
			var layer = layui.layer,//弹层
				table = layui.table,//表格
				$ = layui.jquery,
				laytpl = layui.laytpl,
				form = layui.form,
				laydate = layui.laydate;
			form.render();

			table.render({
				elem: '#List'
				, height:$(window).height()-108
				, limit:1000
				, data:FlowsReadData.Data
				, cols: [[
					{field: 'UUID', title: '识别码'}
					, {field: 'CnName', title: '姓名'}
					, {field: 'Phone', title: '客户手机'}
					, {field: 'Group', title: '校区'}
					, {field: 'Type', title: '类型'}
					, {field: 'ExecutorName', title: '执行人'}
				]]
				, id: "TableId"
			});
			form.on('submit(AddFormButton)', function (data) {
				var PhoneNum = data.field.Phone;
				if(CheckPhone("IndexPhone")){}else {
					return false;
				}
				$.ajax({
					url:"<{SPURL c = 'CRM' a = 'CustomersRead' SPSubmit=''}>",
					data: {
						Form: {Phone:PhoneNum}
					},
					success: function (data) {
						data = JSON.parse(data);
						if (data.Status == "1") {
							if(data.Data.length != 0){
								data.Data[0].JiaoYan = 1;
							}else {
								data.Data[0] = {
									"UUID":"",
									"CnName":"",
									"Phone":PhoneNum,
									"Time":""
								};
							}
							laytpl($("#AddNewStuTpl").html()).render(data.Data[0], function (html) {
								var AddLayer = layer.open({
									type: 1,
									title: '新增',
									content: html,
									area: ['80%',"80%"],
									btn: ['提交', '取消'],
									yes: function (index, layero) {
										form.on('submit(AddButton)', function (data) {
											console.log(data.field);
											CreatData2(data.field,"<{SPURL c = 'CRM' a = 'FlowCreat' SPSubmit=''}>");
										});
										$('div[lay-filter="AddForm"]').find('button[lay-submit]').click();
									},
									success: function () {
										laydate.render({
											elem: '#TimeSelect',
											type:"date"
										});
										$.when(
											$.ajax({url:"<{SPURL c = 'Access' a = 'TagsTreesRead' SPSubmit=''}>",data: {Form: {UUID:["1d46kqne893b155e"]}}}),
											$.ajax({url:"<{SPURL c = 'Access' a = 'TagsTreesRead' SPSubmit=''}>",data: {Form: {UUID:["1d4959nad4b0efc4"]}}}),
											$.ajax({url:"<{SPURL c = 'Access' a = 'TagsTreesRead' SPSubmit=''}>",data: {Form: {UUID:["1d46jpta8453f59e"]}}})
										).then(function (Classes,Schools,Types) {
											Classes = JSON.parse(Classes[0]);
											Classes.Data.splice(0,1);

											Schools = JSON.parse(Schools[0]);
											for(var i in Schools.Data){
												Schools.Data[i].CnName = Schools.Data[i].CnName.replace("校区","");
											}
											Schools.Data.splice(0,1);

											Types = JSON.parse(Types[0]);
											Types.Data.splice(0,1);

											laytpl($('#ClassSelectTpl').html()).render(Classes.Data, function (html) {
												$("#Class").append(html);
												form.render('radio');
											});
											laytpl($('#SchoolSelectTpl').html()).render(Schools.Data, function (html) {
												$("#School").append(html);
												form.render('radio');
											});
											laytpl($('#TypeSelectTpl').html()).render(Types.Data, function (html) {
												$("#Type").append(html);
												form.render('radio');
											});
											form.on('radio(Type)', function(data){
												if(data.value == "1d46jqna166a7714"){
													laytpl($("#IntroTpl").html()).render({}, function (html) {
														var IntroLayer = layer.open({
															type: 1,
															title: '校验转介绍',
															content: html,
															area: ['40%',"30%"],
															btn: ['确认'],
															yes: function (index, layero) {
																layer.close(IntroLayer);
															},
															success: function () {
																var Inner = $("#InnerPhoneInput").val();
																$("#AddNew").click(function () {
																	var Inner = $("#InnerPhoneInput").val();
																	$.ajax({url:"<{SPURL c = 'CRM' a = 'CustomersRead' SPSubmit=''}>",data:{Form:{Phone:Inner}}}).done(function (Inner) {
																		Inner = JSON.parse(Inner);
																		console.log(Inner);
																		if(Inner.Status == 1){
																			if(Inner.Data.length == 0){
																				$("#JiaoYanLable").text("介绍人不存在");
																			}else if(Inner.Data.length == 1){
																				$("#JiaoYanLable").text("此转介绍标记为"+Inner.Data[0].CnName+"所有");
																				$("#InnerOwner").val(Inner.Data[0].UUID);
																			}
																		}
																	});
																});
															}
														});
													})
												}
											})
										});
									}
								});
							});
						} else{
							layer.msg(data.Data);
						}
					}
				});
			});
		});
	});

</script>
</body>
</html>
